//
//  LZGrouponPresenter.m
//  laziz_Consumer
//
//  Created by xbnzd on 16/11/19.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZGrouponPresenter.h"
#import "NetworkServiceManager.h"
#import "LZGrabCouponAndFightGroupsDetialModel.h"
#import "MJExtension.h"
#import "LZUserModel.h"
#import "LZGroupListModel.h"
#import "LZGroupDetailModel.h"

extern NSString *customer;

static NSString *kStartGroupUrl = @"/clientgrouponGroup/add";       //用户开团
static NSString *kCouponOrGroupsDetailUrl = @"/clientsalesPromotion/promotionInfo";    //优惠券或者拼团详情
static NSString *kMessageCenterTypeCouponOrGroupsDetailUrl = @"/clientsalesPromotion/promotionInfoBycouponId";    //根据抢卷或拼团id获取优惠详细信息 根据抢卷或拼团id获取优惠详细信息（type 1 优惠卷，type 2 团购）
static NSString *kUseGroupUrl = @"/clientgrouponUser/update";       //使用拼团
static NSString *kJoinGroupUrl = @"/clientgrouponUser/add";         //参加拼团
static NSString *kQuitGroupUrl = @"/clientgrouponUser/delete";      //退团
static NSString *kCancelGroupUrl = @"/clientgrouponGroup/delete";   //取消团
static NSString *kGroupListUrl = @"/clientgrouponGroup/listPageByPromotionId"; //优惠视频团购列表
static NSString *kGroupDetailUrl = @"/clientgrouponGroup/info";    //团购详情

@implementation LZGrouponPresenter

/**
 用户开团
 
 @param groupName 团购名称
 @param sellerId 商户ID
 @param promotionId 视频id
 @param successBlock 成功
 @param failedBlock 失败
 */
+ (void)startGroupWithGroupName:(NSString *)groupName andSellerId:(NSString *)sellerId andPromotionId:(NSString *)promotionId ResultSuccess:(void (^)(NSString *))successBlock ResultFailed:(FailedBlock)failedBlock {
    if ([LZUserModel Instance].userToken.length == 0) {
        [LZBasePresenter pushLoginViewController];
        if (failedBlock) {
            failedBlock(@"用户未登录,请登录后操作");
        }
        
        return;
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kStartGroupUrl];
    NSDictionary *dic = @{
                          @"groupName":groupName,
                          @"sellerId":sellerId,
                          @"promotionId":promotionId,
                          @"token":[LZUserModel Instance].userToken
                          };
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            if (successBlock) {
                successBlock(resultData[@"data"]);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}

/**
 抢券或者拼团详情
 
 @param videoId <#videoId description#>
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getGrabCouponOrFightGroupsDetailWithVideoID:(NSString *)videoId ResultSuccess:(SuccessBlock)successBlock ResultFailed:(FailedBlock)failedBlock
{
    NSString *str = [NSString stringWithFormat:@"%@%@?id=%@",customer,kCouponOrGroupsDetailUrl,videoId];
    
    [[NetworkServiceManager manager] executGetTask:str parameters:nil success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            LZGrabCouponAndFightGroupsDetialModel *m = [LZGrabCouponAndFightGroupsDetialModel mj_objectWithKeyValues:resultData[@"data"]];
            
            if (successBlock) {
                successBlock(m);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}

/**
 根据抢卷或拼团id获取优惠详细信息 根据抢卷或拼团id获取优惠详细信息（type 1 优惠卷，type 2 团购）
 
 @param videoId <#videoId description#>
 @param type <#type description#>
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getMessageCenterTypeGrabCouponOrFightGroupsDetailWithVideoID:(NSString *)videoId andType:(NSString *)type ResultSuccess:(SuccessBlock)successBlock ResultFailed:(FailedBlock)failedBlock
{
    NSString *str = [NSString stringWithFormat:@"%@%@?id=%@&type=%@",customer,kMessageCenterTypeCouponOrGroupsDetailUrl,videoId,type];
    
    [[NetworkServiceManager manager] executGetTask:str parameters:nil success:^(NSURLSessionDataTask *task, id resultData) {
        
        NSDictionary *dic = (NSDictionary *)resultData;
        if ([dic[@"statusCode"] integerValue] == kCodeSuccesss) {
            LZGrabCouponAndFightGroupsDetialModel *m = [LZGrabCouponAndFightGroupsDetialModel mj_objectWithKeyValues:dic[@"data"]];
            
            if (successBlock) {
                successBlock(m);
            }
        }else{
            NSString *str = dic[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}

/**
 使用团购
 
 @param groupId <#groupId description#>
 @param promotionId <#promotionId description#>
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)useGrouponWithGroupID:(NSString *)groupId PromotionID:(NSString *)promotionId ResultSuccess:(void (^)(void))successBlock ResultFailed:(FailedBlock)failedBlock
{
    if ([LZUserModel Instance].userToken.length == 0) {
        [LZBasePresenter pushLoginViewController];
        if (failedBlock) {
            failedBlock(@"用户未登录,请登录后操作");
        }
        
        return;
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kUseGroupUrl];
    NSDictionary *dic = @{
                          @"id":groupId,
                          @"promotionId":promotionId,
                          @"token":[LZUserModel Instance].userToken
                          };
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            if (successBlock) {
                successBlock();
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
    
}



/**
 参加拼团
 
 @param groupId <#groupId description#>
 @param sellerId <#sellerId description#>
 @param promotionId <#promotionId description#>
 @param grouponCount <#grouponCount description#>
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)joinGrouponWithGroupID:(NSString *)groupId SellerID:(NSString *)sellerId PromotionID:(NSString *)promotionId GrouponCount:(NSString *)grouponCount ResultSuccess:(void (^)(id))successBlock ResultFailed:(FailedBlock)failedBlock
{
    if ([LZUserModel Instance].userToken.length == 0) {
        [LZBasePresenter pushLoginViewController];
        if (failedBlock) {
            failedBlock(@"用户未登录,请登录后操作");
        }
        
        return;
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kJoinGroupUrl];
    NSDictionary *dic;
    if (grouponCount.length != 0) {
        dic = @{
                @"groupId" : groupId,
                @"sellerId":sellerId,
                @"promotionId":promotionId,
//                @"grouponCount":grouponCount,
                @"token": [LZUserModel Instance].userToken
                };
    }else{
        dic = @{
                @"groupId" : groupId,
                @"sellerId":sellerId,
                @"promotionId":promotionId,
                @"token": [LZUserModel Instance].userToken
                };
    }
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        
        if (successBlock) {
            successBlock(resultData);
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
    
}

/**
 参团--用户取消参团
 
 @param groupId 参团的团购id
 @param promotionId 优惠id
 @param successBlock 请求成功
 @param failedBlock 请求失败
 */

+(void)quitGrouponWithDisCountNo:(NSString *)disCountNo GroupID:(NSString *)groupId PromotionID:(NSString *)promotionId ResultSuccess:(void (^)(id))successBlock ResultFailed:(FailedBlock)failedBlock {
    if ([LZUserModel Instance].userToken.length == 0) {
        [LZBasePresenter pushLoginViewController];
        if (failedBlock) {
            failedBlock(@"用户未登录,请登录后操作");
        }
        
        return;
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kQuitGroupUrl];
    NSDictionary *dic =@{
                         @"id" : disCountNo,
                         @"groupId" : groupId,
                         @"promotionId":promotionId,
                         @"token": [LZUserModel Instance].userToken
                         };
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        
        if (successBlock) {
            successBlock(resultData);
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}

/**
 开团--用户取消开团
 
 @param groupId 参团的团购id
 @param successBlock 请求成功
 @param failedBlock 请求失败
 */

+ (void)cancelGrouponWithDisCountNo:(NSString *)disCountNo GroupID:(NSString *)groupId ResultSuccess:(void (^)(id))successBlock ResultFailed:(FailedBlock)failedBlock {
    if ([LZUserModel Instance].userToken.length == 0) {
        [LZBasePresenter pushLoginViewController];
        if (failedBlock) {
            failedBlock(@"用户未登录,请登录后操作");
        }
        
        return;
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kCancelGroupUrl];
    NSDictionary *dic =@{
                         @"guId" : disCountNo,
                         @"id" : groupId,
                         @"token": [LZUserModel Instance].userToken
                         };
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        
        if (successBlock) {
            successBlock(resultData);
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}

/**
 团购列表

 @param promotionId <#promotionId description#>
 @param pageNum <#pageNum description#>
 @param pageLimit <#pageLimit description#>
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getVideoGroupListWithPromotionID:(NSString *)promotionId PageNum:(int)pageNum PageLimit:(int)pageLimit TimeStamp:(NSNumber*)timeStamp ResultSuccess:(void (^)(NSArray *,NSNumber *,NSNumber *))successBlock ResultFailed:(FailedBlock)failedBlock
{
    if ([LZUserModel Instance].userToken.length == 0) {
        [LZBasePresenter pushLoginViewController];
        if (failedBlock) {
            failedBlock(@"用户未登录,请登录后操作");
        }
        
        return;
    }
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kGroupListUrl];
    
    NSDictionary *dic = @{
                          @"token": [LZUserModel Instance].userToken,
                          @"promotionId" : promotionId,
                          @"cpage" : [NSNumber numberWithInt:pageNum],
                          @"limit" : [NSNumber numberWithInt:pageLimit],
                          @"timestamp" : timeStamp
                          };
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            
            NSMutableArray *listArrTemp = [NSMutableArray array];
            NSArray *arrTemp = resultData[@"data"][@"list"];
            for (int i = 0; i < arrTemp.count; i ++) {
                NSDictionary *dicTemp = arrTemp[i];
                LZGroupListModel *m = [LZGroupListModel mj_objectWithKeyValues:dicTemp];
                m.grouponCount = [resultData[@"data"][@"grouponCount"] integerValue];
                [listArrTemp addObject:m];
            }
            
            if (successBlock) {
                successBlock(listArrTemp,resultData[@"timestamp"],resultData[@"data"][@"grouponEndTime"]);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}




/**
 团购列表中团购详情
 
 @param groupId <#groupId description#>
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getVideoGroupDetailWithGroupID:(NSString *)groupId ResultSuccess:(void (^)(id,NSNumber *))successBlock ResultFailed:(FailedBlock)failedBlock
{
    if ([LZUserModel Instance].userToken.length == 0) {
        [LZBasePresenter pushLoginViewController];
        if (failedBlock) {
            failedBlock(@"用户未登录,请登录后操作");
        }
        
        return;
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kGroupDetailUrl];
    NSDictionary *dic = @{
                          @"id" : groupId,
                          @"token" : [LZUserModel Instance].userToken
                          };
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([resultData[@"statusCode"] integerValue] == kCodeSuccesss) {
            LZGroupDetailModel *model = [LZGroupDetailModel mj_objectWithKeyValues:resultData[@"data"]];
            if (successBlock) {
                successBlock(model,resultData[@"timestamp"]);
            }
        }else{
            NSString *str = resultData[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }

    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}






@end
